package com.example.bzh.gamecat.ui.live.hot;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.bzh.gamecat.bean.hot.BannerListBean;

import java.util.List;

/**
 * Created by peter on 2016/8/17.
 */
public class HotViewpageAdapter extends PagerAdapter {

    private List<BannerListBean> list;

    public HotViewpageAdapter(List<BannerListBean> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(final ViewGroup container, final int position) {
        ImageView iv = new ImageView(container.getContext());
        Glide.with(container.getContext()).load(list.get(position % list.size()).getImgUrl()).into(iv);
        container.addView(iv);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BannerListBean bannerListBean = list.get(position % list.size());
                Intent intent = new Intent(container.getContext(), DetailActivity.class);
                intent.putExtra("action", bannerListBean.getAction());
                container.getContext().startActivity(intent);
            }
        });
        return iv;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((ImageView) object);
    }
}
